import urllib, subprocess
from BeautifulSoup import BeautifulSoup

URL = 'http://sourceforge.net/'
NAME = 'SourceForge'
VERSION_CONTROL = 'svn'

def get_description(f):
    info = ''
    soup = BeautifulSoup(f)
    description = soup.find('p', {'class' : "pd-txt"}).string
    return description

def search(arg):
    f = urllib.urlopen(URL + '/projects/' + arg)
    if f.getcode() == 200:
        return [{'name' : arg, 'description' : get_description(f.read())}]
    elif f.getcode() == 404:
        return []

def info(arg):
    i = search(arg)
    if i:
        return search(arg)[0]
    else:
        return search(arg)

def download_info(arg):
    f = urllib.urlopen('http://sourceforge.net/projects/'+ arg +'/develop')
    if f.getcode() == 404:
        return ''
    soup = BeautifulSoup(f.read())
    link = soup.find('code').string
    return link

def download(arg, verbose):
    i = download_info(arg)
    if not i:
        return False
    if verbose:
        ret = subprocess.Popen(i, shell=True)
    else:
        ret = subprocess.Popen(i, shell=True, stdout=subprocess.PIPE)
    ret.wait()
    return True
